#coding=utf-8
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
import yaml
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import sessionmaker
import os
import pymysql

class DBConnect():
    pymysql.install_as_MySQLdb()
    app = Flask(__name__)
    #读取数据库配置文件
    dirPath = os.path.abspath(os.path.join(os.path.dirname("DataBaseConnect.yaml"), os.pardir))
    print(dirPath)
    # fs = open(r"./Caps/DataBaseConnect.yaml", encoding="utf-8") # linux
    print(os.path.join(dirPath,"appium_AutoTest", "Caps", "DataBaseConnect.yaml"))
    fs = open(os.path.join(dirPath,"appium_AutoTest", "Caps", "DataBaseConnect.yaml"), encoding="utf-8")
    datas = yaml.load(fs,Loader=yaml.FullLoader)
    for i in datas:
        host = i["host"]
        port = i["port"]
        dataName = i["dataName"]
        dataUser = i["dataUser"]
        dataPwd = i["dataPwd"]
    #建立数据库连接
    engine = create_engine('mysql+mysqldb://{}:{}@{}:{}/{}?charset=utf8'.format(dataUser,dataPwd,host,str(port),dataName))
    Base = declarative_base()
    Session = sessionmaker(engine)
    db_session = Session()
    db = SQLAlchemy(app)